package com.hspedu.homework;

import org.junit.Test;

import java.io.*;
import java.util.Properties;

/**
 * @author LiuJinTao
 * @className HomeWork03
 * @date 2024/07/05 23:27
 * @description TODO
 */
public class HomeWork03 {
    public static void main(String[] args) throws IOException {
        String path = "Src\\dog.properties";
        Dog dog = new Dog();
        Properties properties = new Properties();
        properties.load(new FileReader(path));
        String name = (String) properties.get("name");
        int age = Integer.parseInt(properties.get("age") + "");
        String color = (String) properties.get("color");
        dog.setAge(age);
        dog.setColor(color);
        dog.setName(name);
        System.out.println("dog=" + dog);

        String path1 = "e:\\dog.dat";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path1));
        objectOutputStream.writeObject(dog);
        objectOutputStream.close();
    }

    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String path1 = "e:\\dog.dat";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path1));
        Dog dog = (Dog) objectInputStream.readObject();
        System.out.println(dog);
        objectInputStream.close();
    }
}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    public Dog() {
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
}